package com.wenyu.wenyuyuedu.adapter.circleAdapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.bean.SearchActivityBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017/3/13.
 * 搜索结果用户的展示
 */

public class SearchResultRecyclerAdapter extends RecyclerView.Adapter implements View.OnClickListener {

    private List<SearchActivityBean.UserBean> mUserList = new ArrayList<>();
    private Context context;
    private LayoutInflater mInflater;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public SearchResultRecyclerAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.search_userlist_item, parent, false);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return new MyUserHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Glide.with(context)
                .load(mUserList.get(position).getPath())
                .error(R.drawable.login_logo)
                .into(((MyUserHolder) holder).imageView);
        ((MyUserHolder) holder).userName.setText(mUserList.get(position).getNickname());
        holder.itemView.setTag(mUserList.get(position));
    }

    @Override
    public int getItemCount() {
        return mUserList.size();
    }

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view, (SearchActivityBean.UserBean) view.getTag());
        }
    }

    class MyUserHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView userName;

        public MyUserHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.user_image);
            userName = (TextView) itemView.findViewById(R.id.user_name);
        }
    }

    public void addAll(List<SearchActivityBean.UserBean> userList) {
        mUserList = userList;
        notifyDataSetChanged();
    }

    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, SearchActivityBean.UserBean data);
    }
}
